﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DBTool
{
    using DB_TABLE_NAME = System.String;

    public partial class CharacterGroupTableMeta : TableMeta
    {
        public CharacterGroupTableMeta()
        {
            _createTableName.Add("charac_group");

            _rowGropList.Add( new KeyValuePair<DB_TABLE_NAME, string[]>( "charac_group", character_group ) );
        }

        protected override void ExtraProcessByTable(string exportPath)
        {
            CharacterIdxSplit(exportPath);
        }

        private void CharacterIdxSplit(string exportPath)
        {
            string characterGroupTablePath = exportPath + "/charac_group" + Config._exportFileExt;

            CSVReader characterGroupTable = new CSVReader(characterGroupTablePath);

            File.Delete(characterGroupTablePath);

            // Sync Delete File
            while (true == File.Exists(characterGroupTablePath)) { }

            StreamWriter fileWriter = new StreamWriter(characterGroupTablePath, false, Encoding.UTF8);

            IList<string> rows = characterGroupTable.GetRows();
            string[] titles = characterGroupTable.GetColumnTitle();

            // write title
            fileWriter.WriteLine(rows[0]);

            for (int rowCount = 1; rowCount < characterGroupTable.GetRowCount(); ++rowCount)
            {
                string group = characterGroupTable.Find(rowCount, "grouping");
                string description = characterGroupTable.Find(rowCount, "description");
                string[] characterIdx = characterGroupTable.Find(rowCount, "character_idx").Split(';');

                foreach (string chaIdx in characterIdx)
                {
                    fileWriter.WriteLine(group + "," + description + "," + chaIdx);
                }
            }

            fileWriter.Close();
        }
    }
}
